prep_amces <- function(x){
  
  #x <- amces_bt
  
  est <- x$estimates
  est <- lapply(est, function(x){x <- t(x); x <- as.data.frame(x); x$att <- row.names(x); colnames(x) <- c("AMCE", "SE", "att"); x})
  base <- x$baselines
  base <- lapply(base, function(x){data.frame(AMCE = 0, "SE" = 0, att = x, stringsAsFactors = F)})
  groups <- lapply(names(base), function(x){data.frame(AMCE = NA, "SE" = NA, att = x, stringsAsFactors = F)})
  combo <- purrr::pmap(list(est, base, groups), function(x, y, z){rbind(z, y,x)})
  combo <- purrr::pmap(list(combo, names(combo)), function(x,y){ x$group <- y; x}) 
  combo <- bind_rows(combo)
  combo$plot_name <- combo$att
  combo$plot_name <- gsub("_", " ", combo$plot_name)
  combo$plot_name[combo$plot_name == "wahlkreis"] <- "Wohnt im Wahlkreis seit"
  combo$plot_name[combo$plot_name == "abweichung"] <- "Abweichung von der Parteilinie"
  combo$plot_name[combo$plot_name == "alter"] <- "Alter"
  combo$plot_name[combo$plot_name == "bildung"] <- "Bildung"
  combo$plot_name[combo$plot_name == "erfahrung"] <- "Erfahrung in der Lokalpolitik"
  combo$plot_name[combo$plot_name == "geschlecht"] <- "Geschlecht"
  combo$plot_name[combo$plot_name == "lokalgremien"] <- "Mitarbeit in lokalen Parteigremien"
  combo$plot_name <- gsub(" Berufsausbildung", " + Berufsausbildung", combo$plot_name)
  combo$plot_name <- gsub(" Studium", " + Studium", combo$plot_name)
  combo$plot_name <- factor(combo$plot_name, levels = rev(combo$plot_name))
  combo
}

plot_amce <- function(x, ci = 90){
  
  dfs <- purrr::pmap(list(x, names(x)), function(x,y){x <- prep_amces(x); x$Ebene <- y; x}) 
  
  df <- bind_rows(dfs)

  df$plot_name
  
  ci_faktor = (100 - (100-ci)/2)/100
  
  df$llci <- df$AMCE - df$SE*qnorm(ci_faktor)
  df$ulci <- df$AMCE + df$SE*qnorm(ci_faktor)
  
  type_text <- c('italic', 'italic', 'italic', 'italic', 'bold',
                 'italic', 'italic', 'bold',
                 'italic', 'italic', 'bold',
                 'italic', 'italic', 'italic', 'italic', 'bold',
                 'italic', 'italic', 'italic', 'italic', 'bold',
                 'italic', 'italic', 'italic', 'italic', 'italic', 'bold',
                 'italic', 'italic', 'italic', 'italic', 'bold')
  
  theme(axis.text.x = element_text(face = ))
  
  if(length(names(x)) > 1){
  p <- ggplot(df, aes(x = plot_name, y = AMCE, ymin = llci, ymax = ulci)) +
    geom_hline(aes(yintercept = 0), lty = 2) +
    geom_pointrange(aes(color = Ebene), position = position_dodge(width = 0.9)) +
    coord_flip() +
    theme_bw(base_size = 16) +
    ylab("AMCE") +
    xlab("") +
    theme(legend.position = "bottom") + 
    theme(axis.text.y = element_text(face = type_text))
    
  } else {
    p <- ggplot(df, aes(x = plot_name, y = AMCE, ymin = llci, ymax = ulci)) +
      geom_hline(aes(yintercept = 0), lty = 2) +
      geom_pointrange(aes(color = group), position = position_dodge(width = 0.9), show.legend = F) +
      coord_flip() +
      theme_bw(base_size = 16) +
      ylab("AMCE") +
      xlab("")  + 
      theme(axis.text.y = element_text(face = type_text))
  }
  return(p)
}


